<?php
/**
 * 投票关系
 */

namespace app\vote\model;


use think\Model;

class VoteUserLink extends Model
{
    protected $autoWriteTimestamp = true;
    /**
     * 添加投票关系
     * @param int $userId
     * @param int $voteId
     * @param array $voteOptionIdList
     * @return bool
    */
    public function addLink($userId,$voteId,$voteOptionIdList){
        //判断是否已经投票
        $linkInfo = $this->getLinkInfo($userId,$voteId);
        if(count($linkInfo) > 0){
            return false;
        }
        $data = array();
        foreach ($voteOptionIdList as & $item){
            $data[] = array(
                'user_id' => $userId,
                'vote_id' => $voteId,
                'vote_option_id' => $item,
            );
        }
        return $this->saveAll($data);
    }

    /**
     * 已经投票内容
     * @param int $userId
     * @param int $voteId
     * @return array
    */
    public function getLinkInfo($userId,$voteId){
        $where['user_id'] = $userId;
        $where['vote_id'] = $voteId;
        return $this->where($where)->column('vote_option_id');
    }
}